package com.yuuzy.dao;

import com.alibaba.druid.util.StringUtils;
import com.yuuzy.model.BankCard;
import com.yuuzy.model.DebitCard;

import java.util.List;
@Deprecated
public class DebitCardDAO extends BankCardDAO<DebitCard> {

    /**
     * 查询储蓄卡信息
     *
     * @param debitCard 待查询的储蓄卡实体
     * @return 查询到的储蓄卡实体集合
     */
    public List<DebitCard> queryBankCard(BankCard debitCard) {
        StringBuilder sb = new StringBuilder("select * from debitcard");
        if (!StringUtils.isEmpty(((DebitCard) debitCard).getDebitcard_id())) {
            //通过银行卡号来查询
            sb.append(" and debitcard_id like '%").append(((DebitCard) debitCard).getDebitcard_id()).append("%'");//支持模糊查询
        }
        if (debitCard.getClient_id() != null) {
            //通过客户id来查询
            sb.append(" and client_id like '%").append(debitCard.getClient_id()).append("%'");//支持模糊查询
        }
        String sql = sb.toString().replaceFirst("and", "where");
        return queryMulti(sql, DebitCard.class);
    }

    /**
     * 增加储蓄卡
     *
     * @param debitCard 增加的储蓄卡实体
     * @return 受影响的行数
     */
    public int insertBankCard(BankCard debitCard) {
        String sql = "INSERT INTO finance.debitcard(debitcard_id,debitcard_password,debitcard_limit,debitcard_remain,client_id) VALUES (?,?,?,?,?)";//添加储蓄卡信息
        return update(sql, debitCard.getBankcard_id(), debitCard.getBankcard_password(), debitCard.getBankcard_limit(), debitCard.getBankcard_remain(), debitCard.getClient_id());
    }

    /**
     * 删除储蓄卡
     *
     * @param debitCard 待删除的储蓄卡实体
     * @return 受影响的行数
     */
    public int deleteBankCard(BankCard debitCard) {
        StringBuilder sb = new StringBuilder("DELETE FROM debitcard");//删除id相匹配的储蓄卡
        if (!StringUtils.isEmpty(((DebitCard) debitCard).getDebitcard_id())) {
            //如果id字段存在
            sb.append(" and debitcard_id = '").append(debitCard.getBankcard_id()).append("'");
        }
        String sql = sb.toString().replaceFirst("and", "where");
        return update(sql);
    }

    /**
     * 更改储蓄卡信息
     *
     * @param debitCard 待更改的储蓄卡实体
     * @return 受影响的行数
     */
    public int updateBankCard(BankCard debitCard) {
        //id不能更改
        StringBuilder sb = new StringBuilder("UPDATE debitcard SET");
        if (!StringUtils.isEmpty(((DebitCard) debitCard).getDebitcard_password())) {
            //如果密码字段存在
            sb.append(" ,debitcard_password = '").append(((DebitCard) debitCard).getDebitcard_password()).append("'");
        }
        if (((DebitCard) debitCard).getDebitcard_limit() != null) {
            //如果限额字段存在
            sb.append(" ,debitcard_limit = ").append(((DebitCard) debitCard).getDebitcard_limit());
        }
        if (((DebitCard) debitCard).getDebitcard_remain() != null) {
            //如果储蓄卡余额字段存在
            sb.append(" ,debitcard_remain = ").append(((DebitCard) debitCard).getDebitcard_remain());
        }
        String sql = sb.append(" WHERE debitcard_id = ? AND client_id = ?").toString().replaceFirst(",", "");
        return update(sql, debitCard.getBankcard_id(), debitCard.getClient_id());
    }
}